//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "AnalyticContinuousFixedLookbackEngine.h"
using namespace Cephei::QL::Pricingengines::Lookback;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::CAnalyticContinuousFixedLookbackEngine (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CGeneralizedBlackScholesProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phAnalyticContinuousFixedLookbackEngine = NULL;
#endif
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        _ppAnalyticContinuousFixedLookbackEngine = new boost::shared_ptr<QuantLib::AnalyticContinuousFixedLookbackEngine> (new QuantLib::AnalyticContinuousFixedLookbackEngine ( _process ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::CAnalyticContinuousFixedLookbackEngine (boost::shared_ptr<QuantLib::AnalyticContinuousFixedLookbackEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticContinuousFixedLookbackEngine = NULL;
#endif
	_ppAnalyticContinuousFixedLookbackEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::CAnalyticContinuousFixedLookbackEngine (QuantLib::AnalyticContinuousFixedLookbackEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phAnalyticContinuousFixedLookbackEngine = NULL;
#endif
	_ppAnalyticContinuousFixedLookbackEngine = new boost::shared_ptr<QuantLib::AnalyticContinuousFixedLookbackEngine> (&childNative);
    
    _AnalyticContinuousFixedLookbackEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::CAnalyticContinuousFixedLookbackEngine (CAnalyticContinuousFixedLookbackEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticContinuousFixedLookbackEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAnalyticContinuousFixedLookbackEngine = new boost::shared_ptr<QuantLib::AnalyticContinuousFixedLookbackEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::CAnalyticContinuousFixedLookbackEngine (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticContinuousFixedLookbackEngine = NULL;
#endif
	if (!t->IsSubclassOf(CAnalyticContinuousFixedLookbackEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::CAnalyticContinuousFixedLookbackEngine (QuantLib::Handle<QuantLib::AnalyticContinuousFixedLookbackEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAnalyticContinuousFixedLookbackEngine = &childNative;
	_ppAnalyticContinuousFixedLookbackEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticContinuousFixedLookbackEngine>>(childNative.currentLink());
    
    _AnalyticContinuousFixedLookbackEngineOwner = owner;
}
Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::CAnalyticContinuousFixedLookbackEngine (QuantLib::Handle<QuantLib::AnalyticContinuousFixedLookbackEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phAnalyticContinuousFixedLookbackEngine = &childNative;
	_ppAnalyticContinuousFixedLookbackEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticContinuousFixedLookbackEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::CAnalyticContinuousFixedLookbackEngine (QuantLib::AnalyticContinuousFixedLookbackEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phAnalyticContinuousFixedLookbackEngine = NULL;
#endif
	_ppAnalyticContinuousFixedLookbackEngine = new boost::shared_ptr<QuantLib::AnalyticContinuousFixedLookbackEngine> (new QuantLib::AnalyticContinuousFixedLookbackEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::~CAnalyticContinuousFixedLookbackEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppAnalyticContinuousFixedLookbackEngine != NULL)
    {
	    delete _ppAnalyticContinuousFixedLookbackEngine;
        _ppAnalyticContinuousFixedLookbackEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::!CAnalyticContinuousFixedLookbackEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppAnalyticContinuousFixedLookbackEngine != NULL)
    {
	    delete _ppAnalyticContinuousFixedLookbackEngine;
    }
}
QuantLib::AnalyticContinuousFixedLookbackEngine& Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::GetReference ()
{
    if (_ppAnalyticContinuousFixedLookbackEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppAnalyticContinuousFixedLookbackEngine;
}
boost::shared_ptr<QuantLib::AnalyticContinuousFixedLookbackEngine>& Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::GetShared ()
{
    if (_ppAnalyticContinuousFixedLookbackEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppAnalyticContinuousFixedLookbackEngine;
}
QuantLib::AnalyticContinuousFixedLookbackEngine* Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::GetPointer ()
{
    if (_ppAnalyticContinuousFixedLookbackEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppAnalyticContinuousFixedLookbackEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AnalyticContinuousFixedLookbackEngine>& Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::GetHandle ()
{
	if (_phAnalyticContinuousFixedLookbackEngine == NULL)
	{
		_phAnalyticContinuousFixedLookbackEngine = new Handle<QuantLib::AnalyticContinuousFixedLookbackEngine> (*_ppAnalyticContinuousFixedLookbackEngine);
	}
	return *_phAnalyticContinuousFixedLookbackEngine;
}
#endif
bool Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::HasNative () 
{
	return (_ppAnalyticContinuousFixedLookbackEngine != NULL);
}

Cephei::QL::Pricingengines::Lookback::IAnalyticContinuousFixedLookbackEngine^ Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine::Calculate::get ()
{
    try
    {
    	(*_ppAnalyticContinuousFixedLookbackEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Lookback::IAnalyticContinuousFixedLookbackEngine^ Cephei::QL::Pricingengines::Lookback::CAnalyticContinuousFixedLookbackEngine_Factory::Create (Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process)
{
    return REFNEW CAnalyticContinuousFixedLookbackEngine ( process);
}
